<template>
  <el-card>
    <!-- 1.0 标题 -->
    <template #header>管理员信息</template>
    <!-- 2.0 主要内容 -->
    <ul class="mian">
      <li>
        管理员Id:
        <p>{{ list.id }}</p>
      </li>
      <li>
        账号:
        <p>{{ list.account }}</p>
      </li>
      <li>
        用户组:
        <p>{{ list.userGroup }}</p>
      </li>
      <li>
        创建时间:
        <p>{{ list.ctime | show() }}</p>
      </li>
      <li>
        管理员头像:
        <p>
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-button type="primary" size="mini" @click="Yes">确定</el-button>
        </p>
      </li>
    </ul>
  </el-card>
</template>

<script>
import local from '../../utils/locl.js'
import moment from 'moment'
import { avataredit } from '../../api/users'
export default {
  data() {
    return {
      list: {},
      imageUrl: '',
      imgurl: ''
    }
  },
  created() {
    this.list = JSON.parse(local.get('user'))
  },
  /* 过滤时间 */
  filters: {
    show(str) {
      return moment(str).format('YYYY-MM-DD  HH:mm:ss')
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.imgurl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    /* 修改图片 */
    async Yes() {
      const data = await avataredit({
        imgUrl: this.imgurl
      })
      if (data.code === 0) {
        location.reload()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.mian {
  li {
    display: flex;
    padding: 10px;
    &:not(:nth-last-child(1)) {
      border-bottom: 1px solid #ccc;
    }
    p {
      margin-left: 10px;
      /deep/.el-image {
        width: 60px;
      }
    }
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
/deep/.el-upload {
  border: 1px solid #ccc;
}
/deep/.el-button {
  margin-top: 10px;
}
</style>
